/*
 * Copyright (c) 2015 MONKEYK Information Technology Co. Ltd
 * www.monkeyk.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * MONKEYK Information Technology Co. Ltd ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement you
 * entered into with MONKEYK Information Technology Co. Ltd.
 */
package com.itic.im.core.jdbc;

import com.itic.im.core.constants.MsgTypeEnum;
import com.itic.im.core.jdbc.repository.ImMsgRepository;
import com.itic.im.core.model.ImMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

public class ImMsgRepositoryJdbc implements ImMsgRepository {

    private static ImMsgRowMapper imMsgRowMapper = new ImMsgRowMapper();


    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void saveImMsg(final ImMsg imMsg) {
        final String sql = " INSERT INTO t_im_msg " +
                " (CHAT_TYPE, SEND_UID, RECEIVE_UID, GROUP_ID, MSG_TYPE, MSG, MSG_SHARDING_DATE, `TIME`, MSG_EXT, FILE_NAME, FILE_TYPE, FILE_SIZE, FILE_EXTENSION, FILE_LASTMODIFIED, FILE_LASTMODIFIEDDATE, EXT1, EXT2, EXT3, EXT4, EXT5, EXT6) " +
                " VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ";

        this.jdbcTemplate.update(sql, ps -> {
            ps.setString(1, imMsg.getChatType());
            ps.setString(2, imMsg.getSendUid());

            ps.setString(3, imMsg.getReceiveUid());
            ps.setString(4, imMsg.getGroupId());

            ps.setString(5, MsgTypeEnum.nameOfMsgType(imMsg.getMsgType()).getCode());
            ps.setString(6, imMsg.getMsg());

            ps.setString(7, imMsg.getMsgShardingDate());

            ps.setObject(8, imMsg.getTime());
            ps.setString(9, imMsg.getMsgExt());

            ps.setString(10, imMsg.getFileName());

            ps.setString(11, imMsg.getFileType());
            ps.setString(12, imMsg.getFileSize());

            ps.setString(13, imMsg.getFileExtension());
            ps.setString(14, imMsg.getFileLastmodified());

            ps.setObject(15, imMsg.getFileLastmodifieddate());
            ps.setString(16, imMsg.getExt1());

            ps.setString(17, imMsg.getExt2());
            ps.setString(18, imMsg.getExt3());

            ps.setString(19, imMsg.getExt4());
            ps.setString(20, imMsg.getExt5());

            ps.setString(21, imMsg.getExt6());

        });
    }
}
